package com.ruoyi.app.business.reward;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.app.business.account.LaborAccountBusiness;
import com.ruoyi.app.business.project.ProjectLaborBusiness;
import com.ruoyi.app.config.util.UserUtil;
import com.ruoyi.app.converter.LaborRewardConverter;
import com.ruoyi.app.domain.reward.LaborReward;
import com.ruoyi.app.domain.reward.LaborRewardInfo;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.web.page.CommonPageInfo;
import com.ruoyi.service.project.entity.ProjectEntity;
import com.ruoyi.service.project.entity.ProjectLaborEntity;
import com.ruoyi.service.project.service.ProjectService;
import com.ruoyi.service.reward.entity.LaborRewardEntity;
import com.ruoyi.service.reward.enums.RewardTypeEnum;
import com.ruoyi.service.reward.query.RewardQuery;
import com.ruoyi.service.reward.service.LaborRewardService;
import com.ruoyi.service.user.entity.UserEntity;
import com.ruoyi.service.user.service.UserService;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 作用：奖惩记录
 * </p>
 *
 * @author Zane
 * @since 2025-10-26 20:18
 */
@Component
public class LaborRewardBusiness {

  @Resource
  private LaborRewardService laborRewardService;

  @Resource
  private ProjectLaborBusiness projectLaborBusiness;
  @Resource
  private ProjectService projectService;

  @Resource
  private UserService userService;
  @Resource
  private LaborAccountBusiness laborAccountBusiness;

  public CommonPageInfo<LaborRewardInfo> listRewardLabor(RewardQuery query) {
    final CommonPageInfo<LaborRewardEntity> pageInfo = PageUtils.startPage(query.getPageNum(),
        query.getPageSize());
    pageInfo.initData(laborRewardService.listRewardLabor(query, UserUtil.getUserId()));
    return covertToResult(pageInfo);
  }

  public CommonPageInfo<LaborRewardInfo> listRewardPlatform(RewardQuery query) {
    final CommonPageInfo<LaborRewardEntity> pageInfo = PageUtils.startPage(query.getPageNum(),
        query.getPageSize());
    pageInfo.initData(laborRewardService.listReward(query));
    return covertToResult(pageInfo);
  }

  public void doReward(LaborReward laborReward) {
    final ProjectLaborEntity laborEntity = checkAndGetProjectLabor(laborReward);
    final ProjectEntity project = projectService.getById(laborEntity.getProjectId());
    //转换数据
    final LaborRewardEntity rewardEntity = LaborRewardConverter.INSTANCE.toRewardEntity(
        laborReward);
    rewardEntity
        .setTraceKey(String.valueOf(laborReward.getTraceId()))
        .setLaborId(laborEntity.getLaborId())
        .setProjectId(laborEntity.getProjectId())
        .setProjectName(project.getProjectName())
        .setPostName(laborEntity.getPostName())
        .setAmount(laborReward.getAmount())
        .setChangeTypeName(
            RewardTypeEnum.valueOfCode(laborReward.getChangeType()).getName())
        .setCreateBy(UserUtil.getUserId())
        .setCreateTime(LocalDateTime.now())
        .setUpdateBy(UserUtil.getUserId())
        .setUpdateTime(LocalDateTime.now());
    //更新钱包
    laborAccountBusiness.reward(laborEntity, rewardEntity);
  }

  private ProjectLaborEntity checkAndGetProjectLabor(LaborReward laborReward) {
    RewardTypeEnum.valueOfCode(laborReward.getChangeType());
    //是否做二次奖惩校验,暂时不做
    return projectLaborBusiness.getById(laborReward.getTraceId());
  }

  private CommonPageInfo<LaborRewardInfo> covertToResult(
      CommonPageInfo<LaborRewardEntity> pageInfo) {
    final CommonPageInfo<LaborRewardInfo> result = pageInfo.convert(
        LaborRewardConverter.INSTANCE::toRewardInfo);
    if (CollUtil.isNotEmpty(result.getList())) {
      final List<Long> userIds = result.getList().stream().map(LaborRewardInfo::getCreateBy)
          .collect(Collectors.toList());
      final Map<Long, UserEntity> userMap = userService.userMapByIds(userIds);
      result.getList().forEach(item -> {
        item.setCreateName(userMap.get(item.getCreateBy()).getName());
      });
    }
    return result;
  }


  public Long getRewardCount() {
    return laborRewardService.getRewardCount(UserUtil.getUserId());
  }
}
